package com.huike.pmps.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huike.pmps.model.entity.PO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
public interface PODao extends BaseMapper<PO> {

    /**
     * 批量插入PO数据
     *
     * @param poList
     * @return
     */
    @Insert({
            "<script>",
            "insert into p_po(po,contract_no, customer_name,contract_amount,contract_applicant,contract_delivery_date,region_id,region_name,province_id,province_name,product_type_id,product_type_name,contract_total_time_length,contract_finished_time_length,contract_unfinished_time_length,contract_payment_received,contract_outstanding_payment,copyright_ownership,contract_gross_profit,forecast_gross_profit_rate,estimated_generation_cost,po_state,after_sale_period,import_flag,state,update_time,create_time,po_remark) values ",
            "<foreach collection='poList' item='item' index='index' separator=','>",
            "(#{item.po},#{item.contractNo}, #{item.customerName}, #{item.contractAmount}, #{item.contractApplicant}, #{item.contractDeliveryDate},#{item.regionId},#{item.regionName}, #{item.provinceId}, #{item.provinceName}, #{item.productTypeId}, #{item.productTypeName},#{item.contractTotalTimeLength},#{item.contractFinishedTimeLength},#{item.contractUnfinishedTimeLength},#{item.contractPaymentReceived},#{item.contractOutstandingPayment},#{item.copyrightOwnership},#{item.contractGrossProfit},#{item.forecastGrossProfitRate},#{item.estimatedGenerationCost},#{item.poState},#{item.afterSalePeriod},#{item.importFlag},#{item.state},#{item.updateTime},#{item.createTime},#{item.poRemark})",
            "</foreach>",
            "</script>"
    })
    int insertPOBatch(@Param(value = "poList") List<PO> poList);

    /**
     * 更新合同已完成时长，未完成时长
     *
     * @param poId
     * @return
     */
    @Update({
            "update p_po set contract_finished_time_length=(select sum(end_edition_time_length2) from p_po_course where po_id=#{poId}) where id=#{poId};",
            "update p_po set contract_unfinished_time_length=(contract_total_time_length - contract_finished_time_length) where id=#{poId};"
    })
    int updatePOContractFinishedTimeLengthByPoId(Integer poId);


    /**
     * 更新 po课程，课程供应商服务，结算单，入库单信息,课程产能表
     *
     * @param item
     * @return
     */
    @Update({
            " UPDATE p_po_course                  SET po=#{item.po},region_id=#{item.regionId},region_name=#{item.regionName},province_id=#{item.provinceId},province_name=#{item.provinceName},customer_name=#{item.customerName} WHERE po_id=#{item.id};  ",
            " UPDATE p_po_course_supplier_detail  SET po=#{item.po},region_id=#{item.regionId},region_name=#{item.regionName},province_id=#{item.provinceId},province_name=#{item.provinceName},customer_name=#{item.customerName} WHERE po_id=#{item.id};  ",
            " UPDATE p_supplier_settlement_detail SET po=#{item.po},region_id=#{item.regionId},region_name=#{item.regionName},province_id=#{item.provinceId},province_name=#{item.provinceName},customer_name=#{item.customerName} WHERE po_id=#{item.id};  ",
            " UPDATE p_supplier_warehouse_receipt SET po=#{item.po},region_id=#{item.regionId},region_name=#{item.regionName},province_id=#{item.provinceId},province_name=#{item.provinceName},customer_name=#{item.customerName} WHERE po_id=#{item.id};  ",
            " UPDATE p_course_capacity            SET po=#{item.po},region_id=#{item.regionId},region_name=#{item.regionName},province_id=#{item.provinceId},province_name=#{item.provinceName} WHERE po_id=#{item.id};  ",
    })
    int updatePODependence(@Param("item") PO item);


    /**
     * 根据记录id批量更新po状态
     *
     * @param poList
     * @return
     */
    @Update({
            "<script>",
            "<foreach collection='poList' item='item' index='index' separator=';'>",
            "update p_po set po_state=#{item.poState} where id=#{item.id}",
            "</foreach>",
            "</script>"

    })
    int updatePOState(@Param(value = "poList") List<PO> poList);


    /**
     * 获取所有po
     *
     * @return
     */
    @Select("select po from p_po where import_flag=2 and po is not null")
    List<String> selectAllPONOList();

    /**
     * 当PO 下插入课程或更新PO下的课程，需要更新po的状态
     * PO状态(1进行中、2已完成、3已过期)
     * @param poId
     * @return
     */
//    @Update({
//            " UPDATE p_po SET  po_state=1 WHERE  id=#{poId} AND (SELECT COUNT(*) FROM p_po_course WHERE course_state='进行中' AND po_id=#{poId})>0 ; ",
//            " UPDATE p_po SET  po_state=2 WHERE  id=#{poId} AND (SELECT COUNT(*) FROM p_po_course WHERE course_state='进行中' AND po_id=#{poId})=0 ; ",
//    })
//    int updatePOState(Integer poId);

    @Update({
            " UPDATE p_po SET  po_state=2 WHERE  id=#{poId} ",
    })
    int updatePOState(Integer poId);

    /**
     * add by zcw 2021-07-04
     * 当 PO下插入课程或更新 PO下的课程，需要更新po的状态
     * PO状态(1未启动、2进行中、3已交付、4已验收  5已完结 6 已终止)
     * @param poId
     * @return
     */
    @Update({
            " UPDATE p_po SET  po_state=#{poState} WHERE  id = #{poId}   AND ((SELECT COUNT(*) FROM p_po_course WHERE  po_id=#{poId}) = (SELECT COUNT(*) FROM p_po_course WHERE course_state=#{courseState} and po_id=#{poId}))"
    })
    int updatePOStateNew(Integer poId,Integer poState,String courseState);

}
